<?php

namespace App\Http\Controllers\Home;

use App\Models\Cates;
use App\Models\News;
use App\Models\Pages;
use App\Models\Seos;
use App\Models\Users;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;


class IndexController extends Controller
{
    protected $chanel_id = 1;//设置导航选中标志

    public function __construct()
    {
        view()->share('chanel_id',$this->chanel_id);
        view()->share('news_position',$this->getNewPosition());
        view()->share('news_hot',$this->getNewHot());
        view()->share('authors',$this->getAuthor());
        view()->share('lives_position',$this->getLivePosition());
        view()->share('seo',Seos::seo('SEO-INDEX'));
    }


    public function index(Request $request)
    {
        //查询新闻类目
        $news_category = Cates::categories(1000);
        $params = [
            'catepid'  => (int)$request->get('catepid',0),
            'cateid'  => (int)$request->get('cateid',0),
            'position'  => (int)$request->get('position',0),//为1，则查询所有推荐
            'status'    => News::STATUS_1,
        ];
        $fields = ['id','title','summary','view','created_at','thumb','cateid','author_id','catepid','praise'];
        $lists = News::home($fields,$params,10);
        if($request->ajax()){
            return $this->writeJson(0,$lists);
        }
        return view('home.index.index',compact('news_category'));
    }


    public function search(Request $request)
    {
        $title = trim((string)$request->get('title',''));
//        $fields = ['id','title','summary','view','created_at','thumb','cateid','author_id','catepid','praise'];
//        $params = [
//            'title' => $title,
//            'status' => News::STATUS_1,
//        ];
//        $lists = News::home($fields, $params);
//        $lists = json_encode($lists);
        return view('home.index.search',compact('title'));
    }


    public function about(Request $request)
    {
        $id = (int)$request->get('id',0);
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        $info = Pages::select(['title','content'])->findOrFail($id);
        return view('home.index.about',compact('info'));
    }

    public function refresh(){
        $data = Users::authors();
        return $this->writeJson(0,$data);
    }
}